package com.bbg.amyam.web.service.login;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import com.bbg.amyam.web.mvc.exception.NoSchoolFoundException;
import com.bbg.amyam.web.util.RequestPathParameter;
import com.bbg.amyam.web.util.RequestPathPaser;

public class DelegatingAuthenticationEntryPoint extends
		LoginUrlAuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException authException)
			throws IOException, ServletException {
		
			RequestPathParameter param = null;
			try {
				param = RequestPathPaser.parse(request);
			} catch (NoSchoolFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (param != null) {
				String shortName = param.getSchool().getShortName();
				this.setLoginFormUrl("/" + shortName + "/login");
			}
		
		super.commence(request, response, authException);
	}

}
